// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package gui.itemView
{
    import com.bit101.components.List;
    import com.bit101.components.PushButton;

    import data.vo.LayoutItemVo;
    import data.vo.LayoutItemVoManger;

    import flash.display.MovieClip;
    import flash.events.Event;

    import manger.LogicManger;

    import utils.ConfirmBox;
    import utils.GUIUtils;
    import utils.MessageBox;

    public class LayoutItemViewPanel
    {
        private var mLogicManger:LogicManger;

        private var mComponentMc:MovieClip;

        private var mItemViewList:List;
        private var mEditItemButton:PushButton;
        private var mDeleteItemButton:PushButton;
        private var mCreateItemButton:PushButton;

        public function LayoutItemViewPanel()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_componentMc:MovieClip, _logicManger:LogicManger):void
        {
            mComponentMc = _componentMc;
            mLogicManger = _logicManger;

            mItemViewList = new List();
            GUIUtils.replaceTargetWithPH(mItemViewList, mComponentMc["itemListPH"]);
            mItemViewList.width = mComponentMc["itemListPH"].width;
            mItemViewList.height = mComponentMc["itemListPH"].height;

            mEditItemButton = new PushButton(null, 0, 0, "编辑", onClickEditItemButton);
            GUIUtils.replaceTargetWithPH(mEditItemButton, mComponentMc["editBtnPH"]);

            mDeleteItemButton = new PushButton(null, 0, 0, "删除", onClickDeleteItemButton);
            GUIUtils.replaceTargetWithPH(mDeleteItemButton, mComponentMc["deleteBtnPH"]);

            mCreateItemButton = new PushButton(null, 0, 0, "创建", onClickCreateItemButton);
            GUIUtils.replaceTargetWithPH(mCreateItemButton, mComponentMc["createBtnPH"]);

            addListener();

            refresh();
        }

        //==================================
        //== Public Function
        //==================================

        public function hide():void
        {
            mComponentMc.visible = false;
        }

        public function show():void
        {
            mComponentMc.visible = true;
        }

        public function refresh():void
        {
            mItemViewList.removeAll();
            for each(var itemVo:LayoutItemVo in LayoutItemVoManger.instance.allLayoutItemVoVector)
            {
                mItemViewList.addItem(itemVo.itemID);
            }
        }

        //==================================
        //== Private Function
        //==================================

        private function addListener():void
        {
            mItemViewList.addEventListener(Event.SELECT, onClickItemInList)
        }

        private function onClickItemInList(e:Event):void
        {
        }

        /**
         * 点击编辑按钮
         */
        private function onClickEditItemButton(e:Event):void
        {
            if (mItemViewList.selectedItem == null)
            {
                MessageBox.instance.showMsg("没有选择任何Item");
            }
            else
            {
                mLogicManger.onClickEditExistLayoutItem(mItemViewList.selectedItem as String);
            }
        }

        /**
         * 点击删除按钮
         */
        private function onClickDeleteItemButton(e:Event):void
        {
            if (mItemViewList.selectedItem == null)
            {
                MessageBox.instance.showMsg("没有选择任何Item");
            }
            else
            {
                ConfirmBox.instance.ask("是否要删除Item:\n" + mItemViewList.selectedItem, onConfirmDeleteItem);
            }
        }

        /**
         * 点击创建新Item
         */
        private function onClickCreateItemButton(e:Event):void
        {
            ConfirmBox.instance.ask("是否要创建新的Item?", onConfirmCreateNewItem);
        }

        //==================
        //==确认后逻辑
        //==================

        /**
         * 确认删除Item
         */
        private function onConfirmDeleteItem():void
        {
            mLogicManger.onClickDeleteLayoutItem(mItemViewList.selectedItem as String);
        }

        /**
         * 确认创建Item
         */
        private function onConfirmCreateNewItem():void
        {
            mLogicManger.onClickCreateNewLayoutItem();
        }

    }
}
